package top.hundreds.oasys.controller.FrontController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import top.hundreds.common.crud.bean.PageParam;
import top.hundreds.common.web.annotation.MultiRequestBody;
import top.hundreds.common.web.api.ApiResult;
import top.hundreds.oasys.bean.entity.Role;
import top.hundreds.oasys.bean.entity.RoleMenu;
import top.hundreds.oasys.bean.vo.RoleVo;
import top.hundreds.oasys.service.RoleMenuService;
import top.hundreds.oasys.service.RoleService;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Role)表控制层
 *
 * @author lcf
 * @since 2020-12-02 19:29:32
 */
@RestController
@RequestMapping("/front/role")
public class RoleController {
    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;

    @Resource
    private RoleMenuService roleMenuService;

    /**
     * 获取单个角色信息
     * @param id 角色id
     * @return success
     */
    @GetMapping("{id}")
    public ApiResult<RoleVo> get(@PathVariable("id") Long id) {
        Role role = this.roleService.get(id);
        RoleVo roleVo = this.roleService.buildVo(role);
        return ApiResult.success(roleVo);
    }

    /**
     * 获取角色列表
     * @return 角色列表
     */
    @GetMapping("list")
    public ApiResult<List<RoleVo>> list() {
        List<Role> roleList = this.roleService.queryAll();
        List<RoleVo> roleVoList = this.roleService.buildVoList(roleList);
        return ApiResult.success(roleVoList);
    }

    /**
     * 模糊查询角色
     * @param pageParam 分页参数
     * @return 角色分页
     */
    @PostMapping("getList")
    public ApiResult<List<RoleVo>> list(@MultiRequestBody PageParam<Role> pageParam) {
        Page<Role> rolePage = this.roleService.getPage(pageParam);
        List<Role> roleList = rolePage.getRecords();
        List<RoleVo> roleVoList = this.roleService.buildVoList(roleList);
        System.out.println(roleVoList);
        return ApiResult.success(roleVoList);
    }

    /**
     * 编辑角色信心
     * @param role 角色信息
     * @return success
     */
    @PutMapping("save")
    public ApiResult<String> save(@MultiRequestBody Role role,@MultiRequestBody List<Integer> menuIds) {
        if (role.getId() == null) {
            Role record = this.roleService.insert(role);
            if(menuIds.size() > 0) {
                this.roleService.changeMenu(record.getId(),menuIds);
            }
            return ApiResult.success("添加成功");
        } else {
            this.roleService.update(role);
            if(menuIds.size() > 0 ) {
                this.roleService.changeMenu(role.getId(),menuIds);
            }
            return ApiResult.success("更新成功");
        }
    }

    /**
     * 删除角色
     * @param id 角色id
     * @return success
     */
    @DeleteMapping("remove/{id}")
    public ApiResult<String> remove(@PathVariable("id") Long id) {
        this.roleService.delete(id);
        this.roleMenuService.deleteByRoleId(id);
        return ApiResult.success("删除成功");
    }

    /**
     * 分配角色权限
     *
     * @param roleId 角色id
     * @param menuIds 权限id
     * @return success
     */
    @PostMapping("changeMenu")
    public ApiResult<String> changeMenu(@MultiRequestBody Long roleId, @MultiRequestBody List<Long> menuIds) {
        this.roleMenuService.deleteByRoleId(roleId);
        for (Long menuId : menuIds) {
            this.roleMenuService.insert(RoleMenu.builder().menuId(menuId).roleId(roleId).build());
        }
        return ApiResult.success();
    }

    @GetMapping("setStatus/{id}/{status}")
    public ApiResult<String> setStatus(@PathVariable Long id, @PathVariable Integer status) {
        this.roleService.update(Role.builder().id(id).status(status).build());
        return ApiResult.success();
    }

}
